package com.example.java2e.Controller;

import com.example.java2e.Entity.Driver;
import com.example.java2e.Entity.Manager;
import com.example.java2e.Entity.User;
import com.example.java2e.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.management.DescriptorRead;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// controller 与前端直接交互 接受前端请求去处理
@RestController
public class UserController
{
    @Autowired //自动注入
    private UserService userService;

    @PostMapping("/register")
    public Map<String, Object> register(HttpServletRequest request, @RequestBody Map<String, Object> front_map)//front_map 前端传来的键值对信息
    {

        String identity = (String) front_map.get("identity");
        String tel = (String) front_map.get("tel");
        String password1 = (String) front_map.get("password1");
        String password2 = (String) front_map.get("password2");
        String name = (String) front_map.get("name");
        String id = (String) front_map.get("id");

        Map<String, Object> back_map = new HashMap<>();
        try
        {
            if (identity.equals("1"))
            {
                // 用户
                User temp_user = userService.selectUserById(id);
                User temp_user2 = userService.selectUserByTel(tel);
                if (temp_user != null || temp_user2 != null)
                {
                    back_map.put("code", "用户已存在");
                }
                else if (id.length() != 8)
                {
                    back_map.put("code", "学号长度不正确");
                }
                else if (id.matches("^\\d*$") == false)
                {
                    back_map.put("code", "学号不全为数字");
                }
                else if (tel.length() != 11)
                {
                    back_map.put("code", "手机号长度不正确");
                }
                else if (tel.matches("^\\d*$") == false)
                {
                    back_map.put("code", "手机号不全为数字");
                }
                else if (name.matches("^\\s*$"))
                {
                    back_map.put("code", "姓名不能为空白符");
                }
                else if (!password1.equals(password2))
                {
                    back_map.put("code", "两次密码输入不一致");
                }
                else
                {
                    User new_user = new User(id, tel, name, password1);
                    userService.registerNewUser(new_user);
                    //首先获取session
                    HttpSession session = request.getSession();
                    //往session中存入你想要的东西
                    session.setAttribute("tel", new_user.getTel());
                    session.setAttribute("name", new_user.getName());
                    session.setAttribute("password", new_user.getPassword());
                    session.setAttribute("id", new_user.getId());
                    session.setAttribute("identity", 1);

                    back_map.put("code", "1");
                    back_map.put("identity", identity);
                }
            }
            else
            {
                // 其他身份
                Manager temp_user = userService.selectManagerById(id);
                Manager temp_user2 = userService.selectManagerByTel(tel);
                if (temp_user != null || temp_user2 != null)
                {
                    back_map.put("code", "该工号已注册");
                }
                else if (id.length() != 8)
                {
                    back_map.put("code", "工号长度不正确");
                }
                else if (!id.matches("^[\\dA-Za-z]*$"))
                {
                    back_map.put("code", "工号不全为数字或字母");
                }
                else if (tel.length() != 11)
                {
                    back_map.put("code", "手机号长度不正确");
                }
                else if (!tel.matches("^\\d*$"))
                {
                    back_map.put("code", "手机号不全为数字");
                }
                else if (!password1.equals(password2))
                {
                    back_map.put("code", "两次密码输入不一致");
                }
                else
                {
                    Manager new_user = new Manager(id, password1, tel, name, Integer.parseInt(identity));
                    userService.registerNewManager(new_user);
                    //首先获取session
                    HttpSession session = request.getSession();
                    //往session中存入你想要的东西
                    session.setAttribute("tel", new_user.getTel());
                    session.setAttribute("name", new_user.getName());
                    session.setAttribute("password", new_user.getPassword());
                    session.setAttribute("id", new_user.getId());
                    session.setAttribute("identity", new_user.getIdentity());

                    back_map.put("code", "1");
                    back_map.put("identity", identity);
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "用户注册失败");
        }
        return back_map;
    }

    @PostMapping("/login")
    public Map<String, Object> login(HttpServletRequest request, @RequestBody Map<String, Object> front_map)
    {
        String password = (String) front_map.get("password");
        String id = (String) front_map.get("id");
        Map<String, Object> back_map = new HashMap<>();
        try
        {
            User temp_user = null;
            Manager temp_user2 = null;
            // 手机号登录
            if(id.length() == 11)
            {
                temp_user = userService.selectUserByTel(id);
                temp_user2 = userService.selectManagerByTel(id);
            }
            // id登录
            else if(id.length() == 8)
            {
                temp_user = userService.selectUserById(id);
                temp_user2 = userService.selectManagerById(id);
            }
            else
            {
                back_map.put("code", "id或手机号错误");
            }


            if (temp_user == null && temp_user2 == null)
            {
                back_map.put("code", "用户不存在");
            }
            else
            {
                String correct_password;
                //学生登录
                if(temp_user != null)
                {
                    correct_password = temp_user.getPassword();
                    if (password.equals(correct_password))
                    {
                        //首先获取session
                        HttpSession session = request.getSession();
                        //往session中存入你想要的东西
                        session.setAttribute("tel", temp_user.getTel());
                        session.setAttribute("name", temp_user.getName());
                        session.setAttribute("password", password);
                        session.setAttribute("id", temp_user.getId());
                        session.setAttribute("identity", 1);
                        session.setAttribute("email", temp_user.getEmail());
                        session.setAttribute("address", temp_user.getAddress());
                        back_map.put("code", "1");
                        back_map.put("identity", "1");
                    }
                    else
                    {
                        back_map.put("code", "用户名或密码不正确");
                    }
                }
                //管理员登录
                else if(temp_user2 != null)
                {
                    correct_password = temp_user2.getPassword();
                    if (password.equals(correct_password))
                    {
                        //首先获取session
                        HttpSession session = request.getSession();
                        //往session中存入你想要的东西
                        session.setAttribute("tel", temp_user2.getTel());
                        session.setAttribute("name", temp_user2.getName());
                        session.setAttribute("password", password);
                        session.setAttribute("id", temp_user2.getId());
                        session.setAttribute("identity", temp_user2.getIdentity());
                        back_map.put("code", "1");
                        back_map.put("identity", temp_user2.getIdentity());
                    }
                    else
                    {
                        back_map.put("code", "用户名或密码不正确");
                    }
                }

            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "登录失败");
        }
        return back_map;
    }

    @PostMapping("/lgout")
    public Map<String, Object> lgout(HttpServletRequest request)
    {
        Map<String, Object> back_map = new HashMap<>();
        //? request.session.flush() 不知道对不对应
        request.getSession().invalidate();//使session失效。可以立即使当前会话失效，原来会话中存储的所有对象都不能再被访问。
        back_map.put("code", "退出成功");
        return back_map;
    }

    @PostMapping("/change_user_password")
    public Map<String, Object> change_user_password(HttpServletRequest request, @RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        HttpSession session = request.getSession();
        String id = (String) session.getAttribute("id");
        try
        {
            if(id != null)
            {
                String old_password = (String) front_map.get("old_password");
                String new_password1 = (String) front_map.get("new_password1");
                String new_password2 = (String) front_map.get("new_password2");
                String password = (String) session.getAttribute("password");
                User temp_user = userService.selectUserById(id);
                Manager temp_user2 = userService.selectManagerById(id);
                if(!old_password.equals(password))
                {
                    back_map.put("code","旧密码错误");
                }
                else if(!new_password1.equals(new_password2))
                {
                    back_map.put("code","两次密码不一致");
                }
                else
                {
                    if(temp_user != null)
                    {
                        temp_user.setPassword(new_password1);
                        userService.deleteUserById(id);
                        userService.registerNewUser(temp_user);
                    }
                    else if(temp_user2 != null)
                    {
                        temp_user2.setPassword(new_password1);
                        userService.deleteManagerById(id);
                        userService.registerNewManager(temp_user2);
                    }
                    session.setAttribute("password", new_password1);
                    back_map.put("code","1");
                }
            }
            else
            {
                back_map.put("code","请先登录");
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "密码更改失败");
        }

        return back_map;
    }
    @PostMapping("/change_user_info")
    public Map<String, Object> change_user_info(HttpServletRequest request, @RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        HttpSession session = request.getSession();
        String id = (String) session.getAttribute("id");
        try
        {
            if(id != null)
            {
                User temp_user = userService.selectUserById(id);
                String new_tel = (String) front_map.get("tel");
                String new_email = (String) front_map.get("email");
                String new_address = (String) front_map.get("address");

                if(new_tel != null && new_tel.length() != 11)
                {
                    back_map.put("code","手机号长度不正确");
                }
                else if(new_tel != null && !new_tel.matches("^\\d*$"))
                {
                    back_map.put("code","手机号不全为数字");
                }
                else if(new_email != null && new_email.length() != 0 && !new_email.contains("@"))
                {
                    back_map.put("code","邮箱地址格式不正确");
                }
                else if(new_address != null && new_address.length() != 0 && new_address.matches("^\\s*$"))
                {
                    back_map.put("code","地址不能为空白符");
                }
                else
                {
                    temp_user.setTel(new_tel);
                    temp_user.setEmail(new_email);
                    temp_user.setAddress(new_address);
                    session.setAttribute("tel",new_tel);
                    session.setAttribute("email",new_email);
                    session.setAttribute("address",new_address);
                    userService.deleteUserById(id);
                    userService.registerNewUser(temp_user);
                    back_map.put("code","1");
                    back_map.put("tel",new_tel);
                    back_map.put("email",new_email);
                    back_map.put("address",new_address);
                }
            }
            else
            {
                back_map.put("code","请先登录");
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "更改失败");
        }

        return back_map;
    }

    @PostMapping("/check_user_info")
    public Map<String, Object> check_user_info(HttpServletRequest request)
    {
        Map<String, Object> back_map = new HashMap<>();
        HttpSession session = request.getSession();
        String id = (String) session.getAttribute("id");
        String name = (String) session.getAttribute("name");
        String tel = (String) session.getAttribute("tel");
        String password = (String) session.getAttribute("password");
        System.out.println(session.getAttribute("identity"));
        int identity = (int) session.getAttribute("identity");
        String email = (String) session.getAttribute("email");
        String address = (String) session.getAttribute("address");


        if(identity == 1)
        {
            back_map.put("id",id);
            back_map.put("name",name);
            back_map.put("tel",tel);
            back_map.put("password",password);
            back_map.put("email",email);
            back_map.put("address",address);
        }
        else
        {
            back_map.put("data","none");
        }
        return back_map;
    }

    @PostMapping("/delete_driver")
    public Map<String, Object> delete_driver(@RequestBody Map<String, Object> front_map)
    {

        Map<String, Object> back_map = new HashMap<>();
        try
        {
            String driver_id = (String) front_map.get("id");
            userService.deleteDriverById(driver_id);
            back_map.put("code", "删除司机成功");

            ArrayList<Driver> drivers = userService.selectDriverList();
            for (Driver i : drivers)
            {
                List temp_list = new ArrayList();
                temp_list.add(i.getName());
                temp_list.add(i.getId());
                if(i.getDriver_condition() == 1)
                    temp_list.add("在职");
                else
                    temp_list.add("停职");
                back_map.put(i.getId(),temp_list);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "删除司机失败");
        }
        return back_map;
    }

    @PostMapping("/add_driver")
    public Map<String, Object> add_driver(@RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        String name = (String) front_map.get("name");
        String gender = (String) front_map.get("gender");
        String age = (String) front_map.get("age");
        String id_card = (String) front_map.get("id_card");
        String id = (String) front_map.get("id");
        String tel = (String) front_map.get("tel");
        String time = (String) front_map.get("time");
        String condition = (String) front_map.get("condition");

        if(!age.matches("^\\d*$") || Integer.parseInt(age) < 18)
        {
            back_map.put("code","年龄不合法");
        }
        else if(id_card.length() != 18)
        {
            back_map.put("code","身份证号长度不正确");
        }
        else
        {
            try
            {
                Driver temp_driver = userService.selectDriverById(id);
                if(temp_driver == null)
                {
                    String con;
                    temp_driver = new Driver(id,id_card,Long.parseLong(condition)
                            ,name,tel,Long.parseLong(gender),Long.parseLong(age),time);
                    userService.registerNewDriver(temp_driver);
                    if (Long.parseLong(condition) == 1)
                        con = "在职";
                    else
                        con = "停职";
                    back_map.put("code","1");
                    back_map.put("condition",con);
                }
                else
                {
                    back_map.put("code","司机已存在");
                }
            }
            catch (Exception e)
            {
                System.out.println(e);
                back_map.put("code", "添加司机失败");
            }
        }


        return back_map;
    }

    @PostMapping("/change_driver_info")
    public Map<String, Object> change_driver_info(HttpServletRequest request, @RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        String gender = (String) front_map.get("gender");
        String age = (String) front_map.get("age");
        String name = (String) front_map.get("name");
        String id = (String) front_map.get("id");
        String tel = (String) front_map.get("tel");
        String time = (String) front_map.get("time");
        String condition = (String) front_map.get("condition");

        if(!age.matches("^\\d*$") || Integer.parseInt(age) < 18)
        {
            back_map.put("code","年龄不合法");
        }
        else
        {
            try
            {
                Driver temp_driver = userService.selectDriverById(id);
                if(temp_driver != null)//先删除再添加
                {
                    //Driver temp_driver2 = new Driver(id,id_card,Long.parseLong(condition)
                    //,temp_driver.getName(),tel,Long.parseLong(gender),Long.parseLong(age),time);
                    temp_driver.setGender(Long.parseLong(gender));
                    temp_driver.setAge(Long.parseLong(age));
                    temp_driver.setName(name);
                    temp_driver.setId(id);
                    temp_driver.setTel(tel);
                    temp_driver.setDate_time(time);
                    temp_driver.setDriver_condition(Long.parseLong(condition));
                    userService.deleteDriverById(id);
                    userService.registerNewDriver(temp_driver);


                    ArrayList<Driver> drivers = userService.selectDriverList();
                    for (Driver i : drivers)
                    {
                        List temp_list = new ArrayList();
                        temp_list.add(i.getName());
                        temp_list.add(i.getId());
                        if (i.getDriver_condition() == 1)
                            temp_list.add("在职");
                        else
                            temp_list.add("停职");
                        back_map.put(i.getId(),temp_list);
                    }
                    back_map.put("code","1");
                }
                else
                {
                    back_map.put("code","没有该工号对应的司机");
                }
            }
            catch (Exception e)
            {
                System.out.println(e);
                back_map.put("code", "修改司机信息失败");
            }
        }


        return back_map;
    }

    @PostMapping("/check_driver_info")
    public Map<String, Object> check_driver_info(@RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        try
        {
            ArrayList<Driver> drivers = userService.selectDriverList();
            for (Driver i : drivers)
            {
                List temp_list = new ArrayList();
                temp_list.add(i.getName());
                temp_list.add(i.getId());
                if (i.getDriver_condition() == 1)
                    temp_list.add("在职");
                else
                    temp_list.add("停职");
                back_map.put(i.getId(), temp_list);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "查看失败");
        }

        return back_map;
    }

    @PostMapping("/check_driver_info_detail")
    public Map<String, Object> check_driver_info_detail(@RequestBody Map<String, Object> front_map)
    {
        Map<String, Object> back_map = new HashMap<>();
        try
        {
            String driver_id = (String) front_map.get("id");
            Driver temp_driver = userService.selectDriverById(driver_id);
            String con,sex;
            if (temp_driver.getDriver_condition() == 1)
                con = "在职";
            else
                con = "停职";
            if (temp_driver.getGender() == 1)
                sex = "男";
            else
                sex = "女";

            back_map.put("id", driver_id);
            back_map.put("id_card", temp_driver.getId_card());
            back_map.put("name", temp_driver.getName());
            back_map.put("tel", temp_driver.getTel());
            back_map.put("gender", sex);
            back_map.put("age", temp_driver.getAge());
            back_map.put("date", temp_driver.getDate_time());
            back_map.put("condition", con);
        }
        catch (Exception e)
        {
            System.out.println(e);
            back_map.put("code", "查看失败");
        }
        return back_map;
    }


}
